<?php
namespace Api\Controller;
use Think\Controller;
header("content-type:application/json;charset=utf-8");
class ImgcateController extends SuperController
{

    //添加前置执行
    function _before_add()
    {
        if (IS_POST) {
            if (I('pid')) {
                $model = M(CONTROLLER_NAME);
                $wdata['id'] = I('pid');
                $vo = $model->where($wdata)->find();
                if (!$vo) {
                    $this->ajaxErrReturn('父节点不存在');
                }
            }
        }
    }

    //删除
    function _before_fdel()
    {
        if (IS_POST) {
            $model = M(CONTROLLER_NAME);
            $wdata['pid'] = I('id');
            $count = $model->where($wdata)->count();
            if ($count) {
                //$this->ajaxErrReturn('下面有子分类或规格,不能删除');
            }
        }
    }
  
    //全部数据树形
    function getTreeList()
    {
        $model = D('Imgcate');
        $map = $this->_search();
        $data = $model->getList($map, 10000, 'pid asc,id asc');
        $tree = new \Think\Tree($data['list']);
        $tree->key_field = 'id'; //主键字段名
        $tree->name_field = 'title'; //主键字段名
        $tree->fid_field = 'pid'; //父节点字段名
        $c_list = $tree->get_tree(0);
        $this->ajaxSucReturn($c_list);
    }
    //全部数据数组
    function getArrList(){
        $model = D('Imgcate');
        $wdata['pid'] = 0;
        $wdata['is_delete'] = 0;
        $wdata['status'] = I('status') ? : 3;//默认设计师款式
        $list = $model->where($wdata)->select();
        foreach($list as &$val){
            if($val['child_num']>0){
                $val['childs'] = $this->digui($val);
            }
        }
        $this->ajaxSucReturn($list);
    }

    //递归
    function digui($val){
        $model = D('Imgcate');
        $wdata['pid'] = $val['id'];
        $wdata['is_delete'] =0;
        $dlist = $model->where($wdata)->select() ? : [];
        foreach($dlist as &$dval){
            if($dval['child_num']>0){
                $dval['childs'] = $this->digui($dval);
            }
        }
        return $dlist;
    }
    
    public function index_gongyi(){
        $detail = I('post.');
        $keyword=$detail['keyword'];
        $limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
        $status= $detail['status']? : 0;
        $model = M('Imgcate');
        $where['is_delete']=0;
        $where['status']=$status;
        $where['pid']=0;
        if($keyword) {
            $where['title'] = array('like',"%".$keyword."%");
        }
        $count=$model->where($where)->count();
        $list= $model->where($where)->field('id,title,pid')->order('id asc')->page($page, $limit)->select();
        if($list){
            foreach($list as $k=>$val){
                $list[$k]['level']=1;
                $lis5c=$model->where(['pid'=>$val['id'],'is_delete'=>0])->field('id,title,pid')->order('id desc')->select();
                $list[$k]['counts']=count($lis5c);
                $list[$k]['child_list']=$lis5c;
                if($lis5c){
                       foreach($lis5c as $k1=>$v1){
                            $list[$k]['child_list'][$k1]['level']=2;
                            $list[$k]['child_list'][$k1]['counts']=count($this->get_child($v1['id']));
                            $list[$k]['child_list'][$k1]['children']=$this->get_child($v1['id']);
                       }
                }

            }
        }
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['count']=$count;
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }
  //查询子分类
  function get_child($pid){
    $model = M('Imgcate');
    $lis5c=$model->where(['pid'=>$pid,'is_delete'=>0])->field('id,title,pid')->order('id desc')->select();
    return $lis5c;
  }
  //查询单位列表
  public function unit_list(){
    $model = M('Imgcate');
    $pid=411;
    $list=$model->where(['pid'=>$pid,'status'=>5,'is_delete'=>0])->field('id,title,pid')->order('id desc')->select();
    $data_cc['code'] = 200;
    $data_cc['msg'] = "查询成功";
    $data_cc['data']['list']=$list;
    exit(json_encode($data_cc));
  }
  public function adds(){
    $detail = I('post.');
    $title=$detail['title'];
    $pid = $detail['pid']? : 0;
    $status= $detail['status']? : 0;
    $id= $detail['id']? : 0;
    $gNote= $detail['sid']? : 0;
    $data=[];
    $data['title']=$title;
    $data['status']=$status;
    $data['pid']=$pid;
    $data['gNote']=$gNote;
    $model = M('Imgcate');
    $where['is_delete']=0;
    $where['title']=$title;
    $where['status']=$status;
    $where['pid']=$pid;
    if($gNote){
        $where['gNote']=$gNote;
    }
    if(empty($title)){
        $data_cc['code'] = 0;
        $data_cc['msg'] = "请输入分类名称";
        exit(json_encode($data_cc));
    }
    if($pid){
        $msg="该子分类名称";
    }else{
        $msg="该顶级分类名称";
    }
    $count=$model->where($where)->count();
    if($count){
        $data_cc['code'] = 0;
        $data_cc['msg'] = $msg."已存在";
        exit(json_encode($data_cc));
    }
    if($id){
        $resault=$model->where(['id'=>$id])->save($data);
        if($resault){
            $data_cc['code'] = 200;
            $data_cc['msg'] = "编辑成功";
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = "编辑失败";
        }
        exit(json_encode($data_cc));
    }else{
        $resault=$model->add($data);
        if($resault){
            $data_cc['code'] = 200;
            $data_cc['msg'] = "添加成功";
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = "添加失败";
        }
        exit(json_encode($data_cc));
    }

   }

   public function del_cct(){
        $detail = I('post.');
        $id=$detail['id'];
        $model = M('Imgcate');
        $img= M('img');
        $stf_info = $model->where(['id'=>$id])->find();
        $where['is_delete']=0;
        $where['pid']=$id;
        $pid=$stf_info['pid'];
        $status=$stf_info['status'];
        $count=$model->where($where)->count();
        if(empty($stf_info)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "信息查询失败";
            exit(json_encode($data_cc));
        }
        if($count){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "该分类下有子分类无法删除 .";
            exit(json_encode($data_cc));
        }
        if($status==4){
            $lsit_count=$img->where(['pid'=>$id,'is_delete'=>0])->count();
            if($lsit_count){
                  $data_cc['code'] = 0;
                  $data_cc['msg'] = "该子分类下有产品数据，无法删除。";
                  exit(json_encode($data_cc));
             }
          }
        if($status==2){
        $lsit_count=$img->where(['pid'=>$id,'is_delete'=>0])->count();
        if($lsit_count){
                $data_cc['code'] = 0;
                $data_cc['msg'] = "该子分类下有产品数据，无法删除。";
                exit(json_encode($data_cc));
            }
        } 
        if($status==1){
            $dfaw=$img->where(['pid'=>$id,'is_delete'=>0])->count();
            if($dfaw){
                  $data_cc['code'] = 0;
                  $data_cc['msg'] = "该子分类下有规格产品数据，无法删除。";
                  exit(json_encode($data_cc));
             }
          }
        $resault=$model->where(['id'=>$id])->save(['is_delete'=>1]);
        if($resault){
            $data_cc['code'] = 200;
            $data_cc['msg'] = "删除成功";
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = "删除失败";
        }
        exit(json_encode($data_cc));
   }
    //获取规格分类下的规格列表
    public function getguige_list(){
            $model = M('Imgcate');
            $where['is_delete']=0;
            $where['status']=1;
            $where['pid']=0;
            $list= $model->where($where)->field('id,title,pid')->order('id asc')->select();
            if($list){
                foreach($list as $k=>$val){
                    $lis5c=$model->where(['pid'=>$val['id'],'status'=>5,'is_delete'=>0])->field('id,title,pid')->order('id desc')->select();
                    $list[$k]['child_list']=$lis5c;
                }
            }
            $data_cc['code'] = 200;
            $data_cc['msg'] = "查询成功";
            $data_cc['data']['list']=$list;
            exit(json_encode($data_cc));
    }
    //自由信息里面添加图片
    public function add_zymc(){
        $detail = I('post.');
        $order_id=$detail['order_id'];
        $id=$detail['id'];
        $img=$detail['img'];
        $data['order_id']=$order_id;
        $data['img']=$img;
        $data['desct']=$detail['desct'];
        $data['add_time']=time();
        $model = M('ziyou_img');
        $order = M('production_order');
        $info=$model->where(['order_id'=>$order_id])->find();
        $stf_info = $order->where(['id'=>$order_id])->find();
        if(empty($order_id)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "order_id参数错误";
            exit(json_encode($data_cc));
        }
        if(empty($stf_info)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "订单查询失败";
            exit(json_encode($data_cc));
        }
        if(empty($img)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请上传图片";
            exit(json_encode($data_cc));
        }
        if($info){
            $resault=$model->where(['order_id'=>$order_id])->save($data);
            $msg='编辑';
        }else{
            $resault=$model->add($data);
            $msg='添加';
        }
        if($resault){
            $data_cc['code'] = 200;
            $data_cc['msg'] = $msg."成功";
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = $msg."失败";
        }
        exit(json_encode($data_cc));
    }
    public function list_zymc(){
        $detail = I('post.');
        $order_id=$detail['order_id'];
        $page=$detail['page']?$detail['page']:1;
        $limit=$detail['limit']?$detail['limit']:10;
        $order = M('production_order');
        $model = M('ziyou_img');
        $stf_info = $order->where(['id'=>$order_id])->find();
        if(empty($order_id)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "order_id参数错误";
            exit(json_encode($data_cc));
        }
        if(empty($stf_info)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "订单查询失败";
            exit(json_encode($data_cc));
        }
        $list=[];
        $where['order_id']=$order_id;
        $list= $model->where($where)->field('id,img,desct')->page($page, $limit)->order("id desc")->select();
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }
    public function del_zymc(){
        $detail = I('post.');
        $id=$detail['id'];
        $order_id=$detail['order_id'];
        $img=$detail['img'];
        $model = M('ziyou_img');
        $order = M('production_order');
        $stf_info = $order->where(['id'=>$order_id])->find();
        if(empty($order_id)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "order_id参数错误";
            exit(json_encode($data_cc));
        }
        if(empty($stf_info)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "订单查询失败";
            exit(json_encode($data_cc));
        }
        $stf_info1 = $model->where(['id'=>$id])->find();
        if(empty($stf_info1)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "信息查询失败";
            exit(json_encode($data_cc));
        }
        $resault=$model->where(['id'=>$id])->delete();
        if($resault){
            $data_cc['code'] = 200;
            $data_cc['msg'] = "删除成功";
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = "删除失败";
        }
        exit(json_encode($data_cc));
    }

    //默认面料列表
    public function mianliao_cate_list(){
        $detail = I('post.');
        $keyword=$detail['keyword'];
        $limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
        $model = M('mianliao_cate');
        $where['isdel']=0;
        $where['uid']=0;
        if($keyword) {
            $where['title'] = array('like',"%".$keyword."%");
        }
        $count=$model->where($where)->count();
        $list= $model->where($where)->field('id,title')->order('id desc')->page($page, $limit)->select();
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['count']=$count;
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }
    public function mianliao_list(){
        $detail = I('post.');
        $keyword=$detail['keyword'];
        $limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
        $cate_id = $detail['cate_id']? : 0;
        $model = M('mianliao');
        $where['isdel']=0;
        $where['uid']=0;
        if($keyword) {
            $where['mianliao_name'] = array('like',"%".$keyword."%");
        }
        if($cate_id) {
            $where['cate_id'] = $cate_id;
        }

        $count=$model->where($where)->count();
        $list= $model->where($where)->field('id,cate_id,mianliao_name,mianliao_pic,suppler_price')->order('id desc')->page($page, $limit)->select();
        if($list){
             foreach($list as $k=>$val){
                     $list[$k]['cate_name']=M('mianliao_cate')->where(['id'=>$val['cate_id']])->getField('title');
             }
        }
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['count']=$count;
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }

    public function mianliao_cate_detail(){
        $detail = I('post.');
        $id=$detail['id'];
        $model = M('mianliao_cate');
        $where['id']=$id;
        $where['isdel']=0;
        $where['uid']=0;
        $count=$model->where($where)->count();
        if($count==0){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "信息查询失败";
            exit(json_encode($data_cc));
        }
        $info= $model->where($where)->find();
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']=$info;
        exit(json_encode($data_cc));
    }
    public function mianliao_detail(){
        $detail = I('post.');
        $id=$detail['id'];
        $model = M('mianliao');
        $where['id']=$id;
        $where['uid']=0;
        $where['isdel']=0;
        $count=$model->where($where)->count();
        if($count==0){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "信息查询失败";
            exit(json_encode($data_cc));
        }
        $info= $model->where($where)->find();
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']=$info;
        exit(json_encode($data_cc));
    }
    public function addcate(){
        $detail = I('post.');
        $title=$detail['title'];
        $id= $detail['id']? : 0;
        $data=[];
        $data['title']=$title;
        $data['uid']=0;
        $model = M('mianliao_cate');
        if(empty($title)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入分类名称";
            exit(json_encode($data_cc));
        }
        if($id){
            $resault=$model->where(['id'=>$id])->save($data);
            if($resault){
                $data_cc['code'] = 200;
                $data_cc['msg'] = "编辑成功";
            }else{
                $data_cc['code'] = 0;
                $data_cc['msg'] = "编辑失败";
            }
            exit(json_encode($data_cc));
        }else{
            $resault=$model->add($data);
            if($resault){
                $data_cc['code'] = 200;
                $data_cc['msg'] = "添加成功";
            }else{
                $data_cc['code'] = 0;
                $data_cc['msg'] = "添加失败";
            }
            exit(json_encode($data_cc));
        }
    }
    public function cate_del(){
        $detail = I('post.');
        $model = M('mianliao_cate');
        $id= $detail['id']? : 0;
        $info=$model->where(['id'=>$id,'isdel'=>0])->find();
        if(empty($info)){
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='信息查询失败';
            exit(json_encode($data_cc));    
        }
        $data_cc=[];
        $result=$model->where(['id'=>$id,'isdel'=>0])->save(['isdel'=>1]);
        if ($result) {
            $data_cc=[];
            $data_cc['code']=200;
            $data_cc['data']='';
            $data_cc['msg']='删除成功';
        } else {
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='删除失败';
        }
        exit(json_encode($data_cc));    
    }


    public function add_mianliao(){
        $detail = I('post.');
        $mianliao_name=$detail['mianliao_name'];
        $id= $detail['id']? : 0;
        $mianliao_name= $detail['mianliao_name']? :'';
        $cate_id= $detail['cate_id']? : 0;
        $suppler_price= $detail['suppler_price']? : '';
        $mianliao_pic= $detail['mianliao_pic']? : '';
        $data=[];
        $data['mianliao_name']=$mianliao_name;
        $data['cate_id']=$cate_id;
        $data['suppler_price']=$suppler_price;
        $data['mianliao_pic']=$mianliao_pic;
        $model = M('mianliao');
        if(empty($mianliao_name)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入面料名称";
            exit(json_encode($data_cc));
        }
        if(empty($mianliao_name)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入面料名称";
            exit(json_encode($data_cc));
        }
        if(empty($cate_id)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请选择分类";
            exit(json_encode($data_cc));
        }
        if(empty($suppler_price)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入价格";
            exit(json_encode($data_cc));
        }
        if(empty($mianliao_pic)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请上传面料图片";
            exit(json_encode($data_cc));
        }
        if($id){
            $resault=$model->where(['id'=>$id])->save($data);
            if($resault){
                $data_cc['code'] = 200;
                $data_cc['msg'] = "编辑成功";
            }else{
                $data_cc['code'] = 0;
                $data_cc['msg'] = "编辑失败";
            }
            exit(json_encode($data_cc));
        }else{
            $resault=$model->add($data);
            if($resault){
                $data_cc['code'] = 200;
                $data_cc['msg'] = "添加成功";
            }else{
                $data_cc['code'] = 0;
                $data_cc['msg'] = "添加失败";
            }
            exit(json_encode($data_cc));
        }
    }
    public function minaliao_del(){
        $detail = I('post.');
        $model = M('mianliao');
        $id= $detail['id']? : 0;
        $info=$model->where(['id'=>$id,'isdel'=>0])->find();
        if(empty($info)){
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='信息查询失败';
            exit(json_encode($data_cc));    
        }
        $data_cc=[];
        $result=$model->where(['id'=>$id,'isdel'=>0])->save(['isdel'=>1]);
        if ($result) {
            $data_cc=[];
            $data_cc['code']=200;
            $data_cc['data']='';
            $data_cc['msg']='删除成功';
        } else {
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='删除失败';
        }
        exit(json_encode($data_cc));    
    }

    
}
